package com.woniuxy.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.shop.entity.User;
import com.woniuxy.shop.mapper.UserMapper;
import com.woniuxy.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fanqiwen
 * @since 2023-02-07 11:56:02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public void reg(String username, String password, String repass) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        //判断用户名是否存在
        if (Objects.nonNull(user)) {
            throw new RuntimeException("用户名已经存在");
        }else {
            if (StringUtils.isEmpty(password)||StringUtils.isEmpty(repass)||!Objects.equals(password,repass)){
                throw new RuntimeException("前后密码不一致");
            }
            //验证通过
            User regUser=new User();
            regUser.setPassword(password);
            regUser.setUsername(username);
            userMapper.insert(regUser);
        }


    }

    @Override
    public void loginCheck(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        if (Objects.isNull(user)) {
            throw  new RuntimeException("用户名错误");
        }
        if (!Objects.equals(password,user.getPassword())){
            throw  new RuntimeException("密码错误");
        }

        }

    @Override
    public User getUserByName(String account) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", account);
        return userMapper.selectOne(queryWrapper);
    }

}
